package OfferCode;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @Author Kechelle
 * @Date 2020/9/11 8:57
 * @Version 1.0
 */
public class Code67 {
    public static int strToInt(String str) {
        String strs = str.trim();     //去掉首尾空格
        StringBuffer sb = new StringBuffer();
        for (int i=0;i<strs.length();i++){
            if (strs.charAt(i)=='-' && i==0){
                sb.append("-");
                continue;
            }
            if (strs.charAt(i) == '+' && i==0){
                sb.append("+");
                continue;
            }
            if (strs.charAt(i)>='0' && strs.charAt(i)<='9'){
                sb.append(strs.charAt(i));
                continue;
            }else {
                break;
            }
        }
        int result = 0;
        if (sb.length()==0||(sb.length()==1 && sb.charAt(0)=='-')||(sb.length()==1 && sb.charAt(0)=='+')){
            return result;
        }
        //利用库函数处理数据越界问题
        try {
            result = Integer.parseInt(sb.toString());
        }catch (Exception e){
            if (sb.charAt(0)=='-'){
                return Integer.MIN_VALUE;
            }else {
                return Integer.MAX_VALUE;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String str = "-91283472332";
        System.out.println(Code67.strToInt(str));
    }
}
